<template>
  <div class="affairs-apply">
    <el-form :model="formData" ref="form" :rules="rules" label-width="120px">
      <div class="form-section">
        <h3>事务申请</h3>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="申请编号" prop="applyNo">
              <el-input v-model="formData.applyNo" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请日期" prop="applyDate">
              <el-date-picker
                v-model="formData.applyDate"
                type="date"
                placeholder="选择申请日期"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="申请部门" prop="department">
              <el-select v-model="formData.department" placeholder="请选择部门" style="width: 100%">
                <el-option
                  v-for="dept in departments"
                  :key="dept.value"
                  :label="dept.label"
                  :value="dept.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" prop="applicant">
              <el-input v-model="formData.applicant"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="事务类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择事务类型" style="width: 100%">
            <el-option label="办公环境" value="environment"></el-option>
            <el-option label="设备维护" value="equipment"></el-option>
            <el-option label="物品领用" value="supplies"></el-option>
            <el-option label="其他事务" value="other"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="事务内容" prop="content">
          <el-input
            type="textarea"
            v-model="formData.content"
            :rows="4"
            placeholder="请详细描述事务内容">
          </el-input>
        </el-form-item>

        <el-form-item label="期望完成时间" prop="expectedTime">
          <el-date-picker
            v-model="formData.expectedTime"
            type="datetime"
            placeholder="选择期望完成时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="优先级" prop="priority">
          <el-radio-group v-model="formData.priority">
            <el-radio label="high">高</el-radio>
            <el-radio label="medium">中</el-radio>
            <el-radio label="low">低</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="附件" prop="attachments">
          <el-upload
            action="/api/file/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="5"
            :file-list="formData.attachments">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">可上传相关文件，单个文件不超过10MB</div>
          </el-upload>
        </el-form-item>
      </div>

      <div class="form-actions">
        <el-button type="primary" @click="submitForm">下一步</el-button>
        <el-button @click="resetForm">重置</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'AffairsApply',
  data() {
    return {
      formData: {
        applyNo: this.generateApplyNo(),
        applyDate: new Date(),
        department: '',
        applicant: '',
        type: '',
        content: '',
        expectedTime: '',
        priority: 'medium',
        attachments: []
      },
      departments: [
        { value: 'tech', label: '技术部' },
        { value: 'hr', label: '人力资源部' },
        { value: 'finance', label: '财务部' },
        { value: 'admin', label: '行政部' }
      ],
      rules: {
        department: [{ required: true, message: '请选择申请部门', trigger: 'change' }],
        applicant: [{ required: true, message: '请输入申请人', trigger: 'blur' }],
        type: [{ required: true, message: '请选择事务类型', trigger: 'change' }],
        content: [{ required: true, message: '请输入事务内容', trigger: 'blur' }],
        expectedTime: [{ required: true, message: '请选择期望完成时间', trigger: 'change' }]
      }
    }
  },
  methods: {
    generateApplyNo() {
      const date = new Date()
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0')
      return `XZ${year}${month}${day}${random}`
    },
    handlePreview(file) {
      console.log(file)
    },
    handleRemove(file, fileList) {
      this.formData.attachments = fileList
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('next-step', this.formData)
        }
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.formData.applyNo = this.generateApplyNo()
      this.formData.applyDate = new Date()
      this.formData.attachments = []
    }
  }
}
</script>

<style lang="scss" scoped>
.affairs-apply {
  .form-section {
    margin-bottom: 30px;
    
    h3 {
      margin: 0 0 20px;
      padding-left: 10px;
      border-left: 4px solid #409EFF;
      font-size: 18px;
      font-weight: 500;
      color: #303133;
    }
  }

  .form-actions {
    text-align: center;
    margin-top: 40px;
  }

  .el-upload__tip {
    color: #909399;
  }
}
</style> 